<?php
class cReport
        {
        var $title=""; // heading for the table
        var $type; // p - printable report, s - summary report, '' - normal view
        var $data=array(); // data to be displayed as report
        var $preferred_rows; // no of rows preferred by user per page
        var $no_of_rows; // total no of rows in report
        var $no_of_pages; // total no of pages in report
        var $page_no; // page no of the current page
        var $query_string=array(); // query string array to be passed to the target page of a link in the report page
        var $html=""; //to store html code created by various functions
        var $customization=array();//report customization stored in the database
        var $db;//database object
        var $sql;//sql query generation object
        var $id;//report id unique identifier for the report
        var $code;//report code for programmer's shorthand for the report
        var $name;//report name for display
        var $summary=array();//summary array;
        var $highlight=true;
        var $set_column_heading=true;
        var $split_heading=true;
	var $print_vertical_heading=false;
	var $doc;
	var $xml_file="report.xml";
        var $summary_report=false;


        function restoreDefaults()
                {
                $this->highlight=true;
                $this->set_column_heading=true;
                }

        // we want the no of rows in the data
        function getRowCount()
                {
                $this->no_of_rows=(isset($this->no_of_rows))?$this->no_of_rows:count($this->data);
                }

        function setStyle()
                {
                $this->customization["table_style"]=($this->customization["table_style"])?$this->customization["table_style"]:"width:100%;border-style:solid; border-width:thin; border-collapse:collapse;";
                $this->customization["th_style"]=($this->customization["th_style"])?$this->customization["th_style"]:"border-style:solid; border-width:thin";
                $this->customization["tr_style"]=($this->customization["tr_style"])?$this->customization["tr_style"]:"border-style:solid; border-width:thin";
                $this->customization["column_style"]=($this->customization["column_style"])?$this->customization["column_style"]:array();
                }

        function setDisplayColumnNorder()
                {
                //we want column=>display_name array, it is set by taking from the corresponding report customisation, otherwise we set it to column=>column
                foreach($this->data[0] as $key=>$value)
                        {
                        if(!$this->customization["display_columns"][$key])
                                {
                                $this->customization["display_columns"][$key]=$key;
                                }
                        }
                //we want column1, column2 array for the order of display, if not available in report customisation show the same order returned by db
                $this->customization["display_order"]=array_keys($this->data[0]);
                }


        function formatColumn($column,$data)
                {
                //the data of the column is to be formatted as set in customization based on the type of data of the column
                switch ($this->customization["data_type"][$column])
                        {
                        case 'string':
                                break;
                        case 'date':
				if(trim($data) != '')
                                	$data=$this->showDate($data,$this->customization["format_details"][$column]);
                                break;
                        case 'number':
				list($decimals,$decimal_separator,$thousand_separator) = explode("-",$this->customization["format_details"][$column]);
                                $data=number_format($data,$decimals,$decimal_separator,$thousand_separator);
                                break;
                        }
                //the data of the column is to be changed if set in customization
                if($this->customization["custom_change_details"][$column])
                        {
                        $eq_value_array=explode(",",$this->customization["custom_change_details"][$column]);
                        foreach($eq_value_array as $equivalent_value)
                                {
                                list($display_value, $replace_value)=explode("=",$equivalent_value);
                                $data=(trim($display_value)==trim($data))?$replace_value:$data;
                                }
                        }
                //return nl2br($data);
                return $data;
                }

        // returns the data in horizontal html table
        function createHorizontalTable()
                {
                if ($this->data)
                        {
                        $this->setStyle();
                        $this->html="<table style=\"".$this->customization["table_style"]."\">";
                        if($this->title)
                                $this->html.="<tr><td colspan=\"".count($this->data[0])."\" class=\"link_head\">".$this->title."</td></tr>";

                        $this->setDisplayColumnNorder();

                        if($this->set_column_heading)
                                {
                                foreach($this->customization["display_order"] as $value)
                                        $this->html.="<th class=\"link_head\" style=\"".$this->customization["th_style"]."\">".$this->customization["display_columns"][$value]."</th>";
                                }

                        $i=0;
                        foreach($this->data as $key=>$value)
                                {
                                //want different formatting for alternate rows
                                if($this->highlight)
                                        $class=($i=!$i)?"row1":"row2";
                                $this->html.="<tr class=\"$class\" style=\"".$this->customization["tr_style"]."\">";
                                foreach($this->customization["display_order"] as $column)
                                        {
                                        $this->customization["column_style"][$column]=($this->customization["column_style"][$column])?$this->customization["column_style"][$column]:"text-align:center";
                                        $this->html.="<td style=\"".$this->customization["column_style"][$column]."\">".$this->formatColumn($column,$this->data[$key][$column])."</td>";
                                        }
                                $this->html.="</tr>";
                                }

                        $this->html.="</table>";
                        unset($this->title);
                        return $this->html;
                        }
                }

        // returns the data in vertical table
        function createVerticalTable()
                {
                if ($this->data)
                        {
                        $this->setStyle();
                        $this->html="\n<table style=\"".$this->customization["table_style"]."\">";
                        if($this->title)
                                $this->html.="<tr><td colspan=\"".count($this->data[0])."\" class=\"link_head\">".$this->title."</td></tr>";
                        $this->setDisplayColumnNorder();
                        $i=0;
                        $this->data=flipArrayKeys($this->data);
                        foreach($this->customization["display_order"] as $column)
                                {
                                if($this->highlight)
                                        $class=($i=!$i)?"row1":"row2";
                                $this->html.="\n\t<tr class=\"$class\" style=\"".$this->customization["tr_style"]."\">";
                                if($this->set_column_heading)
					{
					$this->customization["column_style"][$column]=($this->customization["column_style"][$column])?$this->customization["column_style"][$column]:"text-align:left";
                                        $this->html.="\n\t\t<td class=\"link_head1\" style=\"".$this->customization["column_style"][$column]."\"><b>".$this->customization["display_columns"][$column]."</b></td>";
					}
                                foreach($this->data[$column] as $key=>$value)
                                        {
                                        $this->customization["column_style"][$column]=($this->customization["column_style"][$column])?$this->customization["column_style"][$column]:"text-align:left";
                                        $this->html.="\n\t\t<td style=\"".$this->customization["column_style"][$column]."\">".$this->formatColumn($column,$value)."</td>";
                                        }
                                $this->html.="\n\t</tr>";
                                }
                        $this->html.="\n</table>\n";
                        unset($this->title);
                        return $this->html;
                        }
                }

        // prints the data
        function printData()
                {
                echo $this->html;
                unset($this->html);
                }

        function getSummary($arrays,$group,$heading=array())
                {
                if(count($arrays) && count($group))
                        {
                        $head=key($group);
                        $grouped=$this->sortNgroupArray($arrays,array(key($group)=>current($group)),"yes");
                        array_shift($group);
                        foreach($grouped as $key=>$value)
                                {
                                $heading[$head]=$key;
                                $this->getSummary($value,$group,$heading);
                                }
                        }
                else
                        $this->summary[]=array_merge($heading,$this->sumColumnValues($arrays,$this->customization["summary_columns"]));
                }

        function doHorizontalTable()
                {
                if($this->customization["summary_columns"])
                        {
			if($this->getSummary($this->data,$this->customization["summary_group_columns"]))
				$this->data = array_merge($this->data,$this->getSummary($this->data,$this->customization["summary_group_columns"]));
                        }
                $this->createHorizontalTable();
                $this->printData();
                }

        function doVerticalTable()
                {
                if($this->customization["summary_columns"])
                        {
			if($this->getSummary($this->data,$this->customization["summary_group_columns"]))
				$this->data = array_merge($this->data,$this->getSummary($this->data,$this->customization["summary_group_columns"]));
                        }
                $this->createVerticalTable();
                $this->printData();
                }

        function printHorizontalTable($data)
                {
                $this->data=$data;
                $this->createHorizontalTable();
                $this->printData();
                }

	function printVerticalTable($data)
                {
                $this->data=$data;
                $this->createVerticalTable();
                $this->printData();
                }
        function showGroupedReport()
                {
                $this->groupNshowData($this->data,$this->customization["group_columns"],"");
                }

        function groupNshowData($arrays,$group,$heading)
                {
                global $report_details,$summary_details;
                if(count($arrays) && count($group))
                        {
                        $head=key($group);
                        $grouped=$this->sortNgroupArray($arrays,array(key($group)=>current($group)),"yes");
                        array_shift($group);
                        foreach($grouped as $key=>$value)
                                {
                                $heading[0][$head]=$key;
				$value = $this->sortData($value,$this->customization["sort_columns"]);
                                $this->groupNshowData($value,$group,$heading);
                                }
                        }
                else
                        {
			if($this->customization["summary_columns"])
				{
				$this->getSummary($arrays,$this->customization["summary_group_columns"]);
				$total_array = $this->summary;
				unset($this->summary);
				if($total_array && $this->summary_report==true)
					{
					foreach($total_array as $row=>$field)
						{
						$summary_details[] = array_merge($heading[0],$total_array[$row]);
						}
					}
				}

                        if($this->split_heading)
                                {
				if($this->customization)
					$customization = $this->customization;
                                //$this->customization["table_style"]="width:60%;border-style:solid; border-width:thin; border-collapse:collapse;";
                                $this->customization["tr_style"]="border-style:none; border-width:thin; border-collapse:collapse; height:20";

				if($this->print_vertical_heading)
					$this->printverticalTable($heading);
				else
                                	$this->printHorizontalTable($heading);

                                unset($this->customization["table_style"],$this->customization["tr_style"]);
				$this->customization = $customization;
                                }

                        foreach($arrays as $row=>$record)
                                {
                                if($heading)
                                        {
                                        foreach($heading[0] as $grp=>$val)
                                                {
                                                if($this->split_heading)
							{
                                                        unset($arrays[$row][$grp]);
							}
                                                else
							{
                                                        $arrays[$row][$grp]=($row==0)?$arrays[$row][$grp]:"";
							}
                                                }
                                        }
                                }
			if($this->split_heading)
				{
				$arrays=$this->addSummary($arrays);
				$arrays = $this->sortData($arrays,$this->customization["sort_columns"]);
                                $this->printHorizontalTable($arrays);
                                if($heading)
                                        echo "<br>";
				}
                        else
                                {
                                if(isset($report_details))
					{
					$report_details = array_merge($report_details,$this->addSummary($arrays));
					}
                                else
					{
					$report_details = $this->sortData($arrays,$this->customization["sort_columns"]);
					$report_details = $this->addSummary($arrays);
					}
                                }
                        }
                }

        function addSummary($arrays)
                {
                if($this->customization["summary_columns"])
                        {
                        $this->getSummary($arrays,$this->customization["summary_group_columns"]);
                        $temp[0][key($arrays[0])]="<b>Total</b>";
			foreach($this->summary as $key => $value)
				{
				foreach($value as $key1 => $value1)
					{
					$temp[0][$key1].=$value1;
					}
				}
                        $arrays=appendArrays($arrays,$temp,"APPEND");
                        //$arrays=appendArrays($arrays,$this->summary,"APPEND");
                        unset($this->summary);
                        }
                return $arrays;
                }

        function sortData($arrays,$sort)
                {
                if(count($arrays) && count($sort))
                        {
                        $grouped=$this->sortNgroupArray($arrays,array(key($sort)=>current($sort)),"yes");
                        array_shift($sort);
                        $sorted_array=array();
                        foreach($grouped as $key=>$value)
                                {
                                $sorted_array=array_merge($sorted_array,$this->sortData($value,$sort));
                                }
                        return $sorted_array;
                        }
                else
                        return $arrays;
                }

        function sortNgroupArray($arrays,$sort,$group="no")
                {
                $c=count($arrays);
                for($i=0;$i<$c;$i++)
                        $column_wise[$i]=$arrays[$i][key($sort)];
                $is_sorted=(current($sort) == "ascending")?natcasesort($column_wise):arsort($column_wise);
                foreach($column_wise as $index=>$value)
                        {
                        if ($group == "yes")
                                $grouped_arrays[$value][]=$arrays[$index];
                        else
                                $grouped_arrays[]=$arrays[$index];
                        }
                return $grouped_arrays;
                }

        // retrieves the data for the current page
        function getPageRows()
                {
                $this->getRowCount();
                $this->preferred_rows=($this->preferred_rows)?$this->preferred_rows:$this->no_of_rows;
                $this->no_of_pages=ceil($this->no_of_rows / $this->preferred_rows);

                $this->page_no = ($this->page_no > $this->no_of_pages)?$this->no_of_pages:(($this->page_no=="" || $this->page_no<1)?1:$this->page_no);
                $this->data=array_slice($this->data,($this->page_no-1)*$this->preferred_rows,$this->preferred_rows);
                }

        // displays link for navigating thru' the report pages
        function displayPageNavigation()
                {
                $this->query_string["rtype"]=$this->type;
               	echo "<div align=\"center\">Page ".$this->page_no." of ".$this->no_of_pages."</div>";
                switch ($this->type)
                        {
                        case 'p':// printable report
                                echo "<div align=\"right\">";
                                if($this->no_of_pages > 1)
                                        {
					echo "| ";
                                        if($this->page_no > 1)
                                                {
                                                $this->query_string["page_no"]=$this->page_no - 1;
                                                echo "<a href=\"".$_SERVER["PHP_SELF"]."?".$this->buildQueryString()." \" accesskey=\"r\" \">Prev | </a>";
                                                }

                                        if($this->page_no < $this->no_of_pages)
                                                {
                                                $this->query_string["page_no"]=$this->page_no + 1;
                                                echo "<a href=\"".$_SERVER["PHP_SELF"]."?".$this->buildQueryString()." \" accesskey=\"n\">Next |</a>";
                                                }

                                        echo "</div>";
                                        }
                                break;

                        case 's': //summary report
                                break;

                        default: // normal report
                                echo "<div align=\"right\">| ";
				$no_of_splits = ceil($this->no_of_pages/10);
				$page_no = $this->page_no;
				for($j=1;$j<=$no_of_splits;$j++)
					{
					if($page_no <= $j*10)
						{
						$split = $j;
						break;
						}

					}
				if($split > 1)
					{
					$this->query_string["page_no"]=1;
                                        echo "<a href=\"".$_SERVER["PHP_SELF"]."?".$this->buildQueryString()."\" accesskey=\"i\"><font color=\"#0000ff\">First</font> | </a>";

					$this->query_string["page_no"]=($split*10)-10;
                                        echo "<a href=\"".$_SERVER["PHP_SELF"]."?".$this->buildQueryString()."\" accesskey=\"r\"><font color=\"#0000ff\">Prev</font> | </a>";
					}

                                for($i=($split*10)-9;$i<=$split*10;$i++)
                                        {
					if($i>$this->no_of_pages)
						break;
                                        if($this->page_no==$i)
                                                echo "<font size=\"3\">$i </font>| ";
                                        else
                                                {
                                                $this->query_string["page_no"]=$i;
                                                echo "<a href=\"".$_SERVER["PHP_SELF"]."?".$this->buildQueryString()."\">".$i."</a> | ";
                                                }

                                        }

				if($split < $no_of_splits)
					{
					$this->query_string["page_no"]=($split*10)+1;
                                        echo "<a href=\"".$_SERVER["PHP_SELF"]."?".$this->buildQueryString()."\" accesskey=\"n\" ><font color=\"#0000ff\">Next</font> | </a>";

					$this->query_string["page_no"]=$this->no_of_pages;
                                        echo "<a href=\"".$_SERVER["PHP_SELF"]."?".$this->buildQueryString()."\" accesskey=\"l\" ><font color=\"#0000ff\">Last</font> | </a>";
					}
                                echo "</div>";
				$this->query_string["page_no"]=$this->page_no=$page_no;
                        }
                }

         function navigatePage()
                {
                $this->getPageRows();
                $this->displayPageNavigation();
                }

        function showPrintableReportLink()
                {
                //$this->query_string["page_no"]=($_GET["page_no"])?$_GET["page_no"]:1;
                $this->query_string["rtype"]="p";
                echo "<div align=\"right\"><a href=\"".$_SERVER["PHP_SELF"]."?".$this->buildQueryString()."\" target=_BLANK accesskey=\"p\" >Printable Report</a></div>";
                }

        function returnLink($link="",$name="", $target="",$js_event="")
                {
                $query_string="<a href=\"$link";
                $query_string.="?".$this->buildQueryString();
                $query_string.="\"";
                $query_string.=($target)?" target=$target" : "";
		if($js_event)
			{
			foreach($js_event as $key => $value)
				{
					$query_string.=$key."=".$value;
				}
			}
                $query_string.="><b>$name</b></a>";
                return $query_string;
                }

        function buildQueryString()
                {
                if ($this->query_string)
                        {
                        foreach ($this->query_string as $key => $value)
                                $temp[]=$key."=".$value;
				return implode('&',$temp);
                        }

                }

        function setNA($string='--')
                {
                $this->setValue($this->data,$string);
                }

        function setValue(&$array,$string="--")
                {
                if($array)
                        {
                        foreach($array as $key=>$value)
                                {
                                foreach($value as $column=>$data)
                                        $temp[$key][$column]=($data == NULL || strcmp($data,"")==0)?$string:$data;
                                }
                        $array=$temp;
                        unset($temp);
                        }
                }

        function showDate($given_date,$format="d-M-Y")
                {
                $date_array=explode('-', $given_date);
                $stamp=mktime(0,0,0,$date_array[1], $date_array[2], $date_array[0]);
                return (date ($format, $stamp));
                }
        function getReportDetails1()
                {
		if(!$this->customization["id"] && $this->code && $_SESSION["guser_id"])
                        {
                        $this->sql->column=array("gurc.report_customization_id");
                        $this->sql->table="gen_user_report_customization gurc";
                        $this->sql->join_condition[]="left join gen_master_report_customization using(report_customization_id)";
                        $this->sql->join_condition[]="join sys_master_report smr using(report_id)";
                        $this->sql->condition[]="smr.report_code='".$this->code."'";
                        $this->sql->condition[]="gurc.user_id='".$_SESSION["guser_id"]."'";
                        $this->sql->generateSelectQuery();
                        $this->db->query=$this->sql->query;
                        $this->db->runQuery();
                        $report_customization_array=$this->db->returnArrays();
                        $this->customization["id"]=$report_customization_array[0]["report_customization_id"];
                        }
                $this->sql->column=array("report_customization_id","report_customization_desc[1] as report_title","primary_table", "report_id","report_name[1]","privilege_id","permission","get_columns(group_order) as group_columns","get_columns(sort_order) as sort_columns","get_columns(summary_group_order) as summary_group_columns","get_columns(summary_column) as summary_columns");
                $this->sql->table="sys_master_report";
                $this->sql->join_condition[]="left join gen_master_report_customization using(report_id)";
                $this->sql->condition[]="report_code='".$this->code."'";
                $this->sql->condition[]=($this->customization["id"])?"report_customization_id=".$this->customization["id"]:"is_default='t'";
                $this->sql->generateSelectQuery();
                $this->db->query=$this->sql->query;
                $this->db->runQuery();
                $report_array=$this->db->returnArrays();
                if(strchr($_SESSION["guser_privilege"][$_SESSION["gcompany_id"]][$report_array[0]["privilege_id"]],$report_array[0]["permission"]))
                        {
                        $this->id=$report_array[0]["report_id"];
                        $this->customization["id"]=$report_array[0]["report_customization_id"];
                        $this->customization["report_title"] = $report_array[0]["report_title"];
                        $this->customization["primary_table"] = $report_array[0]["primary_table"];
                        $this->customization["group_columns"]=$this->setGroupNSortOrder(explode(',',$report_array[0]["group_columns"]));
                        $this->customization["sort_columns"]=$this->setGroupNSortOrder(explode(',',$report_array[0]["sort_columns"]));
                        $this->customization["summary_group_columns"]=$this->setGroupNSortOrder(explode(',',$report_array[0]["summary_group_columns"]));
                        $this->customization["summary_columns"]=($report_array[0]["summary_columns"]!="")?explode(',',$report_array[0]["summary_columns"]):"";
                        $this->getReportColumns();
                        }
                unset($report_customization_array,$report_array);
		}

        function getReportDetails()
                {
		if($this->xml_file)
			{
			$this->parseXmlFile();
			return;
			}
                }

        function getReportColumns()
                {
                $this->sql->table="sys_master_report_columns smrc";
                $this->sql->column[]="*";
                $this->sql->column[]="get_column_condition(condition_id) as condition";
                $this->sql->join_condition[]="left join gen_master_column_customization using(column_id)";
                $this->sql->condition[]="report_id='".$this->id."'";
                $this->sql->condition[]="report_customization_id='".$this->customization["id"]."'";
		$this->sql->condition[]="display_order is not null";
                $this->sql->generateSelectQuery();
                $this->db->query=$this->sql->query;
                $this->db->runQuery();
                $rep_col_array=$this->db->returnArrays();

                $c=count($rep_col_array);
                for($i=0;$i<$c;$i++)
                        {
                        if(strchr($_SESSION["guser_privilege"][$_SESSION["gcompany_id"]][$rep_col_array[$i]["privilege_id"]],$rep_col_array[$i]["permission"]))
                                {
                                $display_name=$rep_col_array[$i]["display_name"];
                                $script_variable_name=$rep_col_array[$i]["script_variable_name"];
				$this->customization["script_variable_name"][] =$script_variable_name;
                                if($rep_col_array[$i]["fetch_column"] == 't')
                                        {
					if($rep_col_array[$i]["display_order"] > 0)
						{
						$this->customization["fetch_column"][] =html_entity_decode(stripslashes($rep_col_array[$i]["db_column_name"]))." as \"".$script_variable_name."\"";
						}
					if(isset($rep_col_array[$i]["condition"]))
                                                {
                                                $rep_col_array[$i]["condition_id"] = ereg_replace("[{|}]","",$rep_col_array[$i]["condition_id"]);
                                                $condition_id_array=explode(',',$rep_col_array[$i]["condition_id"]);
                                                $condition_array=explode(',',$rep_col_array[$i]["condition"]);
                                                reset($condition_id_array);
                                                reset($condition_array);
                                                foreach($condition_id_array as $condition_id)
                                                        {
                                                        $condition["'".$condition_id."'"]=current($condition_array);
                                                        next($condition_array);
                                                        }
                                                }
                                        if($rep_col_array[$i]["filter"])
                                                $filter[]=$rep_col_array[$i]["db_column_name"]."='".$rep_col_array[$i]["filter"]."'";
                                        }

                                if($rep_col_array[$i]["display_order"] > 0)
                                        {
                                        $this->customization["display_order"][]=$script_variable_name;
                                        $this->customization["display_columns"][$script_variable_name]=$display_name;
                                        $this->customization["data_type"][$script_variable_name]=$rep_col_array[$i]["data_type"];
                                        if($rep_col_array[$i]["format_details"])
                                                $this->customization["format_details"][$script_variable_name]=$rep_col_array[$i]["format_details"];
                                        if($rep_col_array[$i]["style"])
                                                $this->customization["column_style"][$script_variable_name]=$rep_col_array[$i]["style"];
                                        if($rep_col_array[$i]["custom_change_details"])
                                                $this->customization["custom_change_details"][$script_variable_name]=$rep_col_array[$i]["custom_change_details"];
                                        }
/*				elseif($rep_col_array[$i]["display_order"] < 0)
					{
					unset($this->customization["fetch_column"]);
					}*/
                                }
                        }
		if(isset($condition))
                        {
                        //ksort($condition);
                        //reset($condition);
                        }

                $this->customization["condition"]=(is_array($condition))?array_values($condition):"";
                if(isset($filter))
                        $this->customization["filter"][]=implode(' and ',$filter);
                }

        function setGroupNSortOrder($array)
                {
                if(!$array[0]){return;}
                foreach($array as $value)
                        {
                        $farray[$value]='ascending';
                        }
                return $farray;
                }

        function sumColumnValues($arrays,$column_array)
                {
              $c=count($arrays);
	      $sum=array();
              foreach($column_array as $column)
                        {
                        for($i=0;$i<$c;$i++)
                                $sum[$column]+=$arrays[$i][$column];
                        }
                	return $sum;
                }

	function parseXmlFile()
			{
			$this->doc=DOMDocument::load($this->xml_file) or die("Error in reading XML file");
			$xpath = new DOMXPath($this->doc);
			$entries = $xpath->query('/report/*');
			foreach($entries as $entry)
				$this->customization["privilege"][$entry->getAttributeNode('privilege')->value] = $entry->getAttributeNode('permission')->value;
			$entries = $xpath->query('/report/code[@name = "'.$this->code.'"]/column');
			foreach ($entries as $entry)
				{
				$this->customization["script_variable_name"][] = $entry->getAttributeNode('name')->value;
				$this->customization["script_db_name"][$entry->getAttributeNode('name')->value]=$entry->getElementsByTagName("db_column")->item(0)->nodeValue;
				//if($entry->getElementsByTagName("db_column")->item(0)->nodeValue)
				if($entry->getAttributeNode('fetch_column')->value=="true")
					{
					$this->customization["fetch_column"][]=$entry->getElementsByTagName("db_column")->item(0)->nodeValue.' as "'.$entry->getAttributeNode('name')->value.'"';
					}
				$this->customization["data_type"][$entry->getAttributeNode('name')->value]=$entry->getAttributeNode('data_type')->value;
				}
			$this->customization["id"]=($this->customization["id"])?$this->customization["id"]:"default";

			$this->customization["report_title"] = $this->title=$xpath->query('/report/code[@name = "'.$this->code.'"]/customization[@id="'.$this->customization["id"].'"]')->item(0)->getAttribute("name");
			$entries = $xpath->query('/report/code[@name = "'.$this->code.'"]/customization[@id="'.$this->customization["id"].'"]/stylesheet/*');
			foreach ($entries as $entry)
				{
				if($entry->getAttributeNode('column')->value)
					$this->customization["column_style"][$entry->getAttributeNode('column')->value]=$entry->getAttributeNode('style')->value;
				else
					$this->customization[$entry->tagName."_style"]=$entry->getAttributeNode('style')->value;
				}
			$entries = $xpath->query('/report/code[@name = "'.$this->code.'"]/customization[@id="'.$this->customization["id"].'"]/primary_table/*');
			foreach($entries as $entry)
				{
				if($entry->getAttributeNode('name')->value)
					$this->customization["primary_table"] = $entry->getAttributeNode('name')->value;
				}
			$entries = $xpath->query('/report/code[@name = "'.$this->code.'"]/customization[@id="'.$this->customization["id"].'"]/join_condition/*');
			foreach($entries as $entry)
				{
				if($entry->getAttributeNode('value')->value)
					$this->customization["condition"] = $entry->getAttributeNode('value')->value;
				}

			$entries = $xpath->query('/report/code[@name = "'.$this->code.'"]/customization[@id="'.$this->customization["id"].'"]/apply/*');
			foreach ($entries as $entry)
				$this->customization[$entry->tagName."_columns"][$entry->getAttributeNode('column')->value]=$entry->getAttributeNode('order')->value;

			$entries = $xpath->query('/report/code[@name = "'.$this->code.'"]/customization[@id="'.$this->customization["id"].'"]/column');
			foreach ($entries as $entry)
				{
				$this->customization["display_columns"][$entry->getAttributeNode('name')->value]=$entry->getElementsByTagName("display_name")->item(0)->nodeValue;
				$this->customization["format_details"][$entry->getAttributeNode('name')->value]=$entry->getElementsByTagName("format")->item(0)->nodeValue;

			if($entry->getElementsByTagName("filter")->item(0)->nodeValue)
				{
				$this->customization["filter"][$entry->getAttributeNode('name')->value]=$entry->getElementsByTagName("filter")->item(0)->nodeValue;
				}

			$this->customization["custom_change_details"][$entry->getAttributeNode('name')->value]=$entry->getElementsByTagName("custom_change")->item(0)->nodeValue;
				$this->customization["display_order"][]=$entry->getAttributeNode('name')->value;
				if($entry->getElementsByTagName("summary")->item(0)->nodeValue)
					$this->customization["summary_columns"][]=$entry->getAttributeNode('name')->value;
				}
			}
	function showWidgetReport($path)
		{
		$file_content= "<html>
				<head>
				<link href=\"runtime/styles/xp/grid.css\" rel=\"stylesheet\" type=\"text/css\" ></link>
				<script src=\"runtime/lib/grid.js\"></script>
				<style>
				.active-controls-grid {height: 100%; font: menu;}";
                if ($this->data)
                        {
                        $col=0;
				foreach($this->data[0] as $column=>$value)
                                        {
					$file_content.=".active-column-".$col;
					$style_string=($this->customization["column_style"][$column])?$this->customization["column_style"][$column]:"text-align:center";
					$file_content.=" {".$style_string."}";
					$col++;
                                        }
			$file_content.=".active-grid-column {border-right: 1px solid threedlightshadow;}
					.active-grid-row {border-bottom: 1px solid threedlightshadow;}
					</style>
					<head>
					<body>
					<script>";

                        $this->setDisplayColumnNorder();
				$file_content.="var myColumns = [";
				$column_count=0;
                                foreach($this->customization["display_order"] as $value)
					{
					if($column_count!=0)
						$file_content.=",\"".$this->customization["display_columns"][$value]."\"";
					else
						$file_content.="\"".$this->customization["display_columns"][$value]."\"";
					$column_count++;
					}
				$file_content.="];";
			$file_content.="var myData = [";
			$row=0;
                        foreach($this->data as $key=>$value)
                                {
				$loop_count=0;
				$file_content.="[";
                                foreach($this->customization["display_order"] as $column)
                                        {
					if($loop_count!=0)
						$file_content.=",\"".addslashes($this->formatColumn($column,$this->data[$key][$column]))."\"";
					else
                                        	$file_content.="\"".addslashes($this->formatColumn($column,$this->data[$key][$column]))."\"";
					$loop_count++;
                                        }
				$loop_count=0;
				$row++;
                                $file_content.="],";
                                }
			$file_content.="];";
			}

		$file_content.="</script></head><body><script>var obj = new Active.Controls.Grid;

		obj.setRowProperty(\"count\", $row);
		obj.setColumnProperty(\"count\",$column_count);

		obj.setDataProperty(\"text\", function(i, j){return myData[i][j]});
		obj.setColumnProperty(\"text\", function(i){return myColumns[i]});

		obj.setRowHeaderWidth(\"28px\");
		obj.setColumnHeaderHeight(\"20px\");

		obj.setAction(\"click\", function(src){window.status = src.getItemProperty(\"text\")});

		document.write(obj);

		</script></body></html>";
		$file_name=$path."includes/active/file.html";
		$handle = fopen($file_name,"w+");
		fwrite($handle,$file_content);
		fclose($handle);
		}
	}

?>
